<?php

namespace App\Http\Controllers\H5\Shop;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\ShopAccount;
use App\Common\Model\ShopPayUnline;
use App\Common\Model\ShopUserStatus;
use App\Common\Service\ShopAccountService;
use App\Http\Controllers\Controller;
use App\Model\Btree;
use Illuminate\Http\Request;
use \Log;

class UserController extends Controller
{

    public $accountService;
    public $shopPayUnline;
    public $btree;
    /**
     * H5Controller constructor.
     * @param $articleService
     */
    public function __construct(ShopAccountService $accountService,ShopPayUnline $shopPayUnline,Btree $btree)
    {
        $this->accountService = $accountService;
        $this->shopPayUnline = $shopPayUnline;
        $this->btree = $btree;
    }


    public function userinfo(Request $request) {
        $uid = $request->get('uid');
        $accountInfo = $this->accountService->userInfo($uid);
        if(empty($accountInfo)) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }
        $shopUserStatus = $this->accountService->getUserStatus($uid);
        if(empty($shopUserStatus)) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        //默认填写基本信息
        $workFlowCode = $this->accountService->workFlow($shopUserStatus);
//        $workFlowCode = 0;

        //用户状态
        $userStatus = [
            'recode' => $workFlowCode,
        ];
        //返回该用户的所有线下审核的单据
        $userNum = $this->shopPayUnline->allUsersNum($uid);
        //接点人信息
        $linkNode = $this->btree->findOwerLinkNode($uid);
        if(empty($linkNode)){
            $linkNode = '尚未操作';
        }
        $data = [
            'account_info' => $accountInfo,
            'user_status'  => $userStatus,
            'user_num'=>$userNum,
            'link_name'=>$linkNode
        ];

        return ApiResponse($data);
    }

    /**
     * http://api.aixinjituan.com/Api/shop/register
     * 基本信息填写页
     * @param Request $request
     * @throws AixinException
     */
    public function register(Request $request)
    {

        $uid = $request->get('uid');
        $name = $request->get('name');
        $level = $request->get('level');

        if (empty($uid) || empty($name) || empty($level)) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

        $changeData = [
            'name' => $name,
            'level' => $level,
            'update_time' => time(),
        ];

        $isSave = $this->accountService->register($uid, $changeData);
        if (empty($isSave)) {
            throw new AixinException(AixinExceptionCode::RESULT_NULL, "注册失败！");
        }

        $userStatus = $this->accountService->getUserStatus($uid);
        if(!empty($userStatus)){
            $isOk = $this->accountService->setUserStatus($uid, ShopUserStatus::USER_BASE_STATUS, ShopUserStatus::STATUS_SUCCESS);
        }else {
            $isOk = $this->accountService->initStatus($uid, [ShopUserStatus::USER_BASE_STATUS => ShopUserStatus::STATUS_SUCCESS]);
        }

        if(empty($isOk) ) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        return ApiResponse();

    }



    /**
     * 等待财务确认页，刷新财务接口
     */
    public function wait_refresh(Request $request, ShopAccountService $accountService) {
        $uid = $request->get('uid');
        $shopUserStatus = $this->accountService ->getUserStatus($uid);
        //判断财务是否确认支付
        $recode = intval($shopUserStatus->vip_status);
        if($shopUserStatus -> vip_status == ShopUserStatus::STATUS_SUCCESS) {
            $recode = ShopUserStatus::STATUS_SUCCESS;
        }


        return ApiResponse(['recode' => $recode]);
    }

    /**
     * 修改姓名
     **/
    public function changeName(Request $request) {

        $uid = $request->get("uid");
        $name = $request->get("name");

        $accountInfo = $this->accountService->userInfo($uid);
        if(empty($accountInfo) && empty($name) ) {
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        $isUpdate = $this->accountService->changeUserName($uid, $name);
        if(empty($isUpdate ) ){
            throw new AixinException(AixinExceptionCode::COMMON_FAILD);
        }

        return ApiResponse();

    }

    public function userStatus(Request $request) {
        $uid = $request->get("uid");
        if (empty($uid) ) {
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        $shopUserStatus = $this->accountService->getUserStatus($uid);
        $workFlowCode = ShopUserStatus::USER_BASE_PAGE;     //默认填写基本信息
        if(!empty($shopUserStatus)) {
            $workFlowCode = $this->accountService->workFlow($shopUserStatus);
        }

        $data = [
            'recode' =>   $workFlowCode,
        ];


        return ApiResponse($data);
    }


}
